<?php

namespace App\Http\Controllers;

use Laravel\Lumen\Routing\Controller as BaseController;
use App\Models\Question;
use App\Models\Type;
use App\Services\TypeAnalyst;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ExportController extends BaseController
{
    protected $map;

    public function __construct()
    {
        $this->map = (new TypeAnalyst(Type::all()))->getMap();
    }

    /**
     * 题目导出页面
     * @auth(2,3)
     */
    public function showDownload()
    {
        ob_start();
        ?>
        <h1>导出题目：请输入起止ID</h1>
        <form action="/export" method="POST">
            <legend>起始</legend>
            <input type="text" name="l">
            <br>
            <legend>结束</legend>
            <input type="text" name="u">
            <br>
            <input type="submit" value="开始下载">
        </form>
        <?php
        $offset = 0;
        $list = '<table><tr><th>ID</th><th>位置</th></tr>';
        while (true) {
            $qs = Question::offset($offset)->limit(1)->get();
            if (count($qs) === 0) {
                break;
            }
            $id = $qs[0]->id;
            $list .= "<tr><th>{$id}</th><th>{$offset}</th></tr>";
            $offset += 100;
        }
        $list .= '</table>';
        echo $list;
        ?>
        <h1>抽取题目题目：请输入类型和总数</h1>
        <form action="/extract" method="POST">
            <legend>类型</legend>
            <input type="text" name="form">
            <br>
            <legend>总数</legend>
            <input type="text" name="total">
            <br>
            <legend>过滤图片题？</legend>
            <input type="text" name="filter_image">
            <br>
            <input type="submit" value="抽取题目">
        </form>
        <?php
        $content = ob_get_clean();
        return $content;
    }

    /**
     * 题目导出
     * @auth(2,3)
     */
    public function export(Request $request)
    {
        try {
            $l = $request->input('l');
            $u = $request->input('u');
            $result = '';
            $questions = Question::with('examStatus')->where('id', '>=', $l)->where('id', '<', $u)->get();
            foreach ($questions as $item) {
                // 题目首行
                $result .= $item->buildDescription($this->map);
            }
            return response($result)->withHeaders([
                'Content-Type' => 'text/plain;charset=UTF-8',
                'Content-Disposition' => 'attachment; filename=issues.txt',
            ]);
        } catch (\Exception $e) {
            return $e;
        }
    }

    /**
     * 抽题
     * @auth(2,3)
     */
    public function extract(Request $request)
    {
        try {
            $questionForm = $request->input('form');
            $limit = $request->input('total');
            $filterImage = $request->input('filter_image') == 'true';
            $result = '';
            $query = $questions = Question::with('examStatus')
                ->where('question_form', $questionForm)
                ->orderBy(DB::raw('RAND()'))
                ->offset(0)->limit($limit);
            if ($filterImage) {
                $query->whereNull('graph');
            }
            $questions = $query->get();
            foreach ($questions as $item) {
                // 题目首行
                $result .= $item->buildDescription($this->map);
                $item->delete();
            }
            return response($result)->withHeaders([
                'Content-Type' => 'text/plain;charset=UTF-8',
                'Content-Disposition' => 'attachment; filename=issues.txt',
            ]);
        } catch (\Exception $e) {
            return $e;
        }
    }
}
